package com.xzx.mr.reducejoin;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/26 21:49
 */
public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {
    private Text outputKey = new Text();
    private TableBean outputValue = new TableBean();
    private String name;

    //提前准备好文件名(表名)
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        name = inputSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if ("order.txt".equals(name)) {// 订单表 1001	01	1
            String[] split = line.split("\t");
            outputValue.setId(split[0]);
            outputValue.setpId(split[1]);
            outputValue.setAmount(Integer.parseInt(split[2]));

            outputKey.set(split[1]);
        } else {// 产品表 01	小米
            String[] split = line.split("\t");
            outputValue.setpId(split[0]);
            outputValue.setpName(split[1]);

            outputKey.set(split[0]);
        }

        outputValue.setFlag(name);
        context.write(outputKey, outputValue);
    }
}
